package com.zhuss.core.processor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.user.CreateUserRoleBo;
import com.zhuss.bean.bo.core.user.DeleteUserRoleBo;
import com.zhuss.bean.bo.core.role.RoleUserBindBo;
import com.zhuss.bean.vo.core.role.DetailRoleVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.RoleDao;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.UserRoleDeleteType;
import com.zhuss.exception.AabException;
import com.zhuss.util.JsonUtil;

/**
 * 绑定角色的用户 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("roleUserBind")
public class RoleUserBind extends AbstractProcessor<Object, RoleUserBindBo> implements Processor<Object, RoleUserBindBo> {

    @Autowired
    private RoleDao roleDao;
    
    @Autowired
	@Qualifier("roleRead")
	private RoleRead roleRead;
    
    @Autowired
	@Qualifier("userRoleRead")
	private UserRoleRead userRoleRead;
    
    @Autowired
	@Qualifier("userRoleDelete")
	private Processor<Object, DeleteUserRoleBo> userRoleDelete;
    
    @Autowired
	@Qualifier("userRoleCreate")
	private Processor<Object, CreateUserRoleBo> userRoleCreate; 
    
    
    /**
     * 校验
     */
    @Override
    public void check(RoleUserBindBo bo) throws AabException {
    	
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(RoleUserBindBo bo) throws AabException {
    	
    	Long roleId = bo.getRoleId();
    	List<Long> paramList = bo.getUserIdList();
    	@SuppressWarnings("unchecked")
		List<Long> dbList = (List<Long>)userRoleRead.listUserId(roleId);
    	List<Long> addList = new ArrayList<Long>(paramList);
    	List<Long> removeList = new ArrayList<Long>(dbList);
    	
    	removeList.removeAll(paramList);
    	addList.removeAll(dbList);
    	
    	if (!removeList.isEmpty()) {
    		DeleteUserRoleBo param = new DeleteUserRoleBo();
    		param.setType(UserRoleDeleteType.根据角色删除用户角色关系.key());
    		param.setRoleId(roleId);
    		param.setUserIdList(removeList);
    		
    		param.setModifierId(bo.getConcurrentUserId());
    		param.setModifier(bo.getConcurrentUserName());
    		param.setModifyTime(new Date());
    		userRoleDelete.process(param);
    	}
    	
    	for (Long userId : addList) {
    		CreateUserRoleBo param = new CreateUserRoleBo();
    		BeanUtils.copyProperties(bo, param);
    		param.setUserId(userId);
    		param.setRoleId(roleId);
    		userRoleCreate.process(param);
    	}
    	
    	String name = ((DetailRoleVo) roleRead.detail(roleId)).getName();
    	bo.setName(name);
    	bo.setAddList(addList);
    	bo.setRemoveList(removeList);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(RoleUserBindBo param) throws AabException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.角色用户绑定.key());
    	record.setTargetId(param.getRoleId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
		return null;
	}
}
